package com.topisv.tms.utils.ttkd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.ExpressConfigure;
import com.topisv.tms.utils.HttpClientUtil;

import net.sf.json.JSONArray;

/**
 * 天天快递消息请求类
 * 
 * @author chenhongtao
 *
 */
public class DayMailSendRequest {
	
	private static final Logger log = LoggerFactory.getLogger(DayMailSendRequest.class);
	

	/**
	 * 获取运单号段消息数据
	 * 
	 * @param expressConfigure
	 * @return
	 */
	public static List<String> SendWayBillMessage(ExpressConfigure expressConfigure) {
		
		List<String> mailNoList= new ArrayList<String>();
		try {
			String charset = "utf-8";
			String url = expressConfigure.getUrl();
			String site = expressConfigure.getCompanyName();
			String cus = expressConfigure.getWarehouseName();
			String password = expressConfigure.getPassword();
			String getCount = expressConfigure.getGetCount();
			String billtype = expressConfigure.getMonthAccount();
			// 请求参数组装
			String paramThreeValue = DayMailMsgBuildUtil.buildWayBillMsg(site,
					cus, password, getCount,billtype);

			HashMap<String, String> paramMap = new HashMap<String, String>();
			paramMap.put(DayMailConstants.PARAM_ONE,DayMailConstants.PARAM_ONE_VALUE);
			paramMap.put(DayMailConstants.PARAM_TWO,DayMailConstants.PARAM_TWO_BILL_VALUE);
			paramMap.put(DayMailConstants.PARAM_THREE, paramThreeValue);

			String data = HttpClientUtil.doPost(url, charset, paramMap);
			log.info("申请电子面单参数："+paramThreeValue);
			log.info("申请天天电子面单号结果：" + data);
			if (StringUtils.isNotBlank(data)) {
				String[] arry = data.split(",");
				for(String numData:arry)
				{
					String[] dataArry = numData.split("-");
					long number;
					long number1 = Long.valueOf(dataArry[0]);
					long number2 = Long.valueOf(dataArry[1]);
					int count = Integer.valueOf(getCount);
					for (int i = 0; i < count; i++) {
						number = number1 + i;
						//log.info("申请天天电子面单号结果：" + i + "==" + number);
						if (number <= number2) {
							mailNoList.add(String.valueOf(number));
						}
					}
				}
				
			}

		} catch (Exception e) {
			log.error("system error", e);
			e.printStackTrace();
		}
		return mailNoList;
	}
	
	/**
	 * 提取订单大头笔数据信息(供打印面单用)
	 * 
	 * @param expressConfigure 快递公司配置对象
	 * @param eo 订单对象
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Map<String, String> SendFaceBillMessage(ExpressConfigure expressConfigure,EdiOrder eo ) {
		
		Map<String, String> resultMap = new HashMap<String, String>();
		try {
			String charset = "utf-8";
			String url = expressConfigure.getUrl();
			String site = expressConfigure.getCompanyName();
			String cus = expressConfigure.getWarehouseName();
			String password = expressConfigure.getPassword();
			
			// data参数数据组装
			Map<String, String> dataMap = new HashMap<String, String>();
			dataMap.put("key", eo.getOrdeno());
			dataMap.put("prov", eo.getProvna());
			dataMap.put("city", eo.getCityna());
			dataMap.put("area", eo.getDistna());
			// 请求参数组装成json数据
			String paramThreeValue = DayMailMsgBuildUtil.bulidFaceBillMsg(site, cus, password, dataMap);
			HashMap<String, String> paramMap = new HashMap<String, String>();
			paramMap.put(DayMailConstants.PARAM_ONE,DayMailConstants.PARAM_ONE_VALUE);
			paramMap.put(DayMailConstants.PARAM_TWO,DayMailConstants.PARAM_TWO__VALUE);
			paramMap.put(DayMailConstants.PARAM_THREE, paramThreeValue);
			
			log.info("url：" + url);
			String data = HttpClientUtil.doPost(url, charset, paramMap);
			// 解析数据json数组数据为map数据
			log.info("申请天天电子面单大头笔结果：" + data);
			// 解析数据json数组数据位map数据
			JSONArray jsonArray = JSONArray.fromObject(data);
			List<Map<String, String>> mapListJson = (List) jsonArray;
			for (int i = 0; i < mapListJson.size(); i++) {
				Map<String, String> obj = mapListJson.get(i);

				for (Entry<String, String> entry : obj.entrySet() ) {
					String strkey = entry.getKey();
					String strval = entry.getValue();
					resultMap.put(strkey, strval);
				}
			}

		} catch (Exception e) {
			log.error("system error", e);
		}
		return resultMap;
	}
}
